//第三方包 Go-MySQL-Driver
//具体如何操作也有点记不清了 可以看看官网
//这个比较好的原因是 引入包比较少 操作简单 但也有问题功能太少了

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/Go-SQL-Driver/MySQL"
	//"time"
)

func main() {
	db, err := sql.Open("mysql", "root:@/vgou3.0s?charset=utf8")
	checkErr(err)

	defer db.Close() //defer遇到异常就关闭

	////插入数据
	//stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
	//checkErr(err)

	//res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09") //防止sql注入
	//checkErr(err)

	//id, err := res.LastInsertId()
	//checkErr(err)

	//fmt.Println("刚刚插入的id==>", id)

	//更新数据
	//stmt, err = db.Prepare("update userinfo set username=? where uid=?")
	//checkErr(err)

	//res, err = stmt.Exec("astaxieupdate", 2)
	//checkErr(err)

	//affect, err := res.RowsAffected()
	//checkErr(err)

	//fmt.Println("修改过的条数==>", affect)

	//res, err := db.Query("update userinfo set username='cc' where uid=1") //这个可以这么用但是返回的内容好像不能解析
	//var uid int
	//var username string
	//var department string
	//var created string
	//err = res.Scan(&uid, &username, &department, &created)
	//fmt.Println(res)
	//fmt.Println(uid)
	//fmt.Println(username)
	//fmt.Println(department)
	//fmt.Println(created)

	////查询数据

	row := db.QueryRow("SELECT * FROM vg_order_info where id = 44")
	var id int
	//var order_sn string
	//var user_id int
	//var order_content string
	row.Scan(&id)
	fmt.Println(id)

	//rows, err := db.Query("SELECT * FROM userinfo where uid =2")
	//checkErr(err)
	//for rows.Next() {
	//	var uid int
	//	var username string
	//	var department string
	//	var created string
	//	err = rows.Scan(&uid, &username, &department, &created)
	//	checkErr(err)
	//	fmt.Println("循环出的数据==>", uid)
	//	fmt.Println(uid)
	//	fmt.Println(username)
	//	fmt.Println(department)
	//	fmt.Println(created)
	//}

	////删除数据
	//stmt, err = db.Prepare("delete from userinfo where uid=?")
	//checkErr(err)

	//res, err = stmt.Exec(id)
	//checkErr(err)

	//affect, err = res.RowsAffected()
	//checkErr(err)

	//fmt.Println(affect)

	db.Close()

}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}
